package org.emet.flex.services
{
	import flash.events.Event;
	import flash.utils.getQualifiedClassName;
	
	public class AMFResponseHandler
	{
		private var _className:String = "";
		private var _error:String = "";
		private var _data:Array = [];
		
		public function AMFResponseHandler()
		{
		}
		
		/**pass in the required class - we need a class name to verify that the class that
		 * returned from the AMF call is of the right type eg, Calendar VO
		 **/
		public function setClassName ( referenceClass:Class ): void {
			try {
				_className = getQualifiedClassName ( referenceClass );
			}
			catch (e:Error) {
				_className = "";
			}
		}
		
		public function get error (  ): String {
			return _error;
		}
		public function set error ( error:String ): void {
			_error = error;
		}
		
		/** always returns an array,even if the result is a single string
		 *  this is so we can ensure consistency of output and also so the
		 *  result can be strongly typed
		 **/
		public function get data (  ): Array {
			return _data;
		}
		
		/**pass in the complete event. This class will process it and output the
		 * required array of VO classes or an error string
		 **/
		public function handleComplete ( e:Event ) : void  {
			
			var eventClassName:String = "";
			
			if ( _className == "" )
			{
				_error = "Internal Error: No classname set";
			}
			else
			{
				//we don't know if we are getting back an array of objects or just the object
				if ( e.currentTarget.lastResult is Array ) {
					processArray ( e.currentTarget.lastResult );
				}else {
					processObject ( e.currentTarget.lastResult );
				}
			}
		}
		
		/**pass in the complete event for a session Query. This class will process it and output the
		 * required array of VO classes or an error string
		 **/
		public function handleSessionComplete ( e:Event ) : void  {
			try
			{
				_data = [e.currentTarget.lastResult];
			}
			catch (e:Error) {
				_error = "Could not get a session string";
			}
		}
		
		private function processArray ( array:Array ) : void  {
			
			try
			{
				if ( getQualifiedClassName ( array[0] ) == _className ){
					
					var err:String = array[0].error;
					
					if ( err == "" || err == null || err == "0" || err == "0/0/0"){
						_data =  array ;
					}
					else {
						_error = err;
					}
					
				}else{
					_error = "No records match the request";
				}
			}
			catch (e:Error) {
				_error = "No records match the request";
			}
		}
		
		private function processObject ( obj:Object ) : void {
			
			try
			{
				if ( getQualifiedClassName ( obj ) == _className ){
					
					var err:String = obj.error;
					
					if ( err == "" || err == null || err == "0" || err == "0/0/0"){
						_data =  [obj] ;
					}
					else {
						_error = err;
					}
					
				}else{
					_error = "No records match the request";
				}
			}
			catch (e:Error) {
				_error = "No records match the request";
			}
			
		}
		
	}

}